﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.Web;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;

namespace POSSystem.BLL
{
    public class Users: _Users
    {
        public Users()
        {
            this.AddColumn("RoleName", typeof(string));
        }

        public string RoleName
        {
            get { return (string)this.GetColumn("RoleName"); }
            set { this.SetColumn("RoleName", value); }
        }

        public static Users GetUserByUserNamePassword(string userName, string password)
        {
            try
            {
                Users usr = new Users();
                usr.Query.AddResultColumn(Users.ColumnNames.ID);
                usr.Where.Deleted.Value = false;
                usr.Where.UserName.Value = userName;
                usr.Where.Password.Value = password;
                usr.Query.Top = 1;
                usr.Query.Load();

                return usr;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Users.cs", "GetUserByUserNamePassword", ex.StackTrace);
                throw ex;
            }
        }

        public static bool IsUserNameExist(string userName, int userID)
        {
            try
            {
                Users usr = new Users();
                usr.Query.AddResultColumn(Users.ColumnNames.ID);
                usr.Where.Deleted.Value = false;
                usr.Where.UserName.Value = userName;
                if (userID >= 0)
                {
                    usr.Where.ID.Value = userID;
                    usr.Where.ID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.NotEqual;
                }
                usr.Query.Top = 1;
                usr.Query.Load();

                return usr.RowCount > 0;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Users.cs", "GetUserByUserNamePassword", ex.StackTrace);
                throw ex;
            }
        }

        public static Users GetAllActiveUsers()
        {
            try
            {
                ListDictionary parameters = new ListDictionary();
                parameters.Add(new SqlParameter("@branchID", SqlDbType.Int, 0), Branches.CurrentBranchID);
                Users usr = new Users();
                usr.LoadFromSql("GetUserWithRoleName", parameters);

                return usr;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Users.cs", "GetUserByUserNamePassword", ex.StackTrace);
                throw ex;
            }
        }

    }
}
